package cn.hxt.org.service.impl;

import cn.hxt.basic.service.impl.BaseServiceImpl;
import cn.hxt.basic.util.PageList;
import cn.hxt.org.domain.Department;
import cn.hxt.org.mapper.DepartmentMapper;
import cn.hxt.org.query.DepartmentQuery;
import cn.hxt.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 新增一条数据
     * @param department
     */
    @Override
    public void add(Department department) {
        departmentMapper.save(department);
        //反正到头来都要调update，我直接把代码放到他里面，我再调他就是
        this.update(department);

    }

    /**
     * 修改一条数据
     * @param department
     */
    @Override
    public void update(Department department) {
        String dirPath = "";
        //判断上级dirPath是否为空。
        if(department.getParent() == null){
            // 为空就是顶级，就只需要显示自己的id即可
            dirPath = "/"+department.getId();
        }else{
            //不为空，那么就不是顶级，就要将他的上级部门id和他的id进行拼接还回
            Department parentId = departmentMapper.loagById(department.getParent().getId());
            //          将父级的id跟自己的id进行拼接
            dirPath = parentId.getId() +"/"+department.getId();
        }
        department.setDirPath(dirPath);
        //再调用update修改回去
        departmentMapper.update(department);
    }



    //查询一二级
    @Override
    public List<Department> findTree() {
        return departmentMapper.findOneAndTwolevelTree();
    }
}
